package many.to.many;

import org.hibernate.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import utils.HibernateUtils;

public class HibernateTest {
	
	private Session session;

	@Before
	public void before() {
		session = HibernateUtils.getSession();
		session.beginTransaction();
	}
	
	@Test
	public void testGET() {
		Category category = session.get(Category.class, 1);
		
		//默认开启延迟加载  
		//这里就算使用fetch=select 也不会使用拒绝使用联机查询
		System.out.println(category.getClass().getName());
		
		System.out.println(category.getItems().size());
	}
	
	
	@Test
	public void testManyToMany() {
		Category category1 = new Category();
		
		category1.setName("CC");
		
		Category category2 = new Category();
		
		category2.setName("BB");
		
		Item item1 = new Item();
		item1.setName("A-JAVA");
		
		Item item2 = new Item();
		item2.setName("A-C#");
		
		category1.getItems().add(item1);
		category1.getItems().add(item2);
		//5条insert 与位置无关，始终是中间表维护关系连接
		
		session.save(item1);
		session.save(item2);
		
		session.save(category1);
		
		
	}
	
	@After
	public void after() {
		session.getTransaction().commit();
		session.close();
	}

}
